/* eslint-disable */

window.WPT = window.WPT || {};
window.WPT.Model = window.WPT.Model || {};

var STOP_PULL_TO_REFRESH = [
 '//w.weipaitang.com/wptIM/im/index.html',
 '//w.weipaitang.com/webApp/discovery'
];
var urlRefresh = window.location.href;
var isDisabled;
for (var i = 0 ; i < STOP_PULL_TO_REFRESH.length ; i++) {
  if (urlRefresh.indexOf(STOP_PULL_TO_REFRESH[i]) > -1) {
         isDisabled = true;
         wxx && wxx.isStopRefresh && wxx.isStopRefresh(false);
         break;
  }
}
wxx && wxx.isStopRefresh && wxx.isStopRefresh(false);


function webLoginSuccess(uriWeb, headimgurlWeb, isSubscribeWeb, sellLevelWeb, buyerLevelWeb, isLoginWeb, hasTelephoneWeb){
    window.WPT = window.WPT||{};
    WPT.userinfo.uri = uriWeb;
    WPT.userinfo.headimgurl = headimgurlWeb;
    WPT.userinfo.isSubscribe = isSubscribeWeb;
    WPT.userinfo.sellLevel = sellLevelWeb;
    WPT.userinfo.buyerLevel = buyerLevelWeb;
    WPT.userinfo.isLogin = isLoginWeb;
    WPT.userinfo.hasTelephone = hasTelephoneWeb;
    window.isSubscribe = isSubscribeWeb;
    window.sellLevel = sellLevelWeb;
    window.buyerLevel = buyerLevelWeb;
    window.isLogin = isLoginWeb;
    window.hasTelephone = hasTelephoneWeb;
}

var successPay = function () {};
var cancelPay = function () {};
var failPay = function () {};
var successCallBackTemp = function () {};
var failCallBackTemp = function () {};
var tmpUrl;
var successDownloadImage = function () {};
var failLogin = function () {};
var successLogin = function () {};
var successLogout = function () {};


function mySuccessLogin(o) {
    if (successLogin) {
        successLogin();
    } else {
        var options = [];
        WPT.loginRefreshUserinfo(function (res) {
            WPT.userinfo.isSubscribe = 1;
        }, options);
    }
}

function myFailLogin(o) {
    failLogin && failLogin();
}

function mySuccessLogout(o) {
    if (successLogout) {
        successLogout();
    } else {
        if ($.os.ios) {
            wptAppSetCookie('userinfo_cookie', '', -1);
        }
        wptAppSetCookie('wptLogoutAction', '/my', 1);
    }
}
function mySuccessPay(){
    var res = new Object();
    successPay(res);
}
function myCancelPay(){
    var res = new Object();
    cancelPay(res);
}
function myFailPay(){
    var res = new Object();
    failPay(res);
}



var successCode = function () {};
function mySuccessCode(code){
     var res = new Object();
     res.errMsg = "scanQRCode:ok";
     res.resultStr = ","+code;
     successCode(res);
}
var successAddress = function () {};
function mySuccessAddress(errMsg,userName,postalCode,provinceName,cityName,countryName,detailInfo,nationalCode,telNumber){
     var res = new Object();
     res.errMsg = errMsg;
     res.userName = userName;
     res.postalCode = postalCode;
     res.provinceName = provinceName;
     res.cityName = cityName;
     res.countryName = countryName;
     res.detailInfo = detailInfo;
     res.nationalCode = nationalCode;
     res.telNumber = telNumber;

     successAddress(res);
}
function wptAboutView(){
     wxx.wptAboutView();
}


var successShare = function () {};
var failShare = function () {};
var clickPage = function () {};

function myClickPage(){
   clickPage();
}

function mySuccessShare(){
   successShare && successShare();
   successShare = false;
}

function myFailShare(){
   failShare();
}

function wptSharePlus(wxShareJson,successShareTemp,failShareTemp,clickPageTemp){
    successShare = successShareTemp;
    failShare = failShareTemp;
    clickPage = clickPageTemp;
    var wxShareJsonTemp = wxShareJson ? JSON.stringify(wxShareJson) : "";
    var noShareArrJsonTemp = "";
    if(wxShareJsonTemp) {
        var noShareArrJsonTemp = wxShareJson.noShareArr ? JSON.stringify(wxShareJson.noShareArr) : "";
    }
    wxx.wptShare(wxShareJsonTemp,noShareArrJsonTemp);
}


function wptShare(noShareArr){
    var wxShareJson = window.wxShare ? JSON.stringify(wxShare) : "";
    var noShareArrJson = noShareArr ? JSON.stringify(noShareArr) : "";
    wxx.wptShare(wxShareJson,noShareArrJson);
}
function wptImageShare(imgUrl,noShareArr){
    var noShareArrJson = noShareArr ? JSON.stringify(noShareArr) : "";
    wxx.wptImageShare(imgUrl,noShareArrJson);
}

var wptQRShareObj = null;
function wptQRShare(o){
    wptQRShareObj = o;
    var tempJson = o ? JSON.stringify(o) : "";
    wxx.wptQRShare(tempJson);
}
function getWptQRShareObj(toFlag) {
    wxx.getWptQRShareObj(wptQRShareObj.imgUrl,toFlag);
}

function triggerChooseImage(){
    $(".mainMenu .publish, .head .publish").trigger("touchend");
}

function checkShare(){
      var wxShareJson = window.wxShare ? JSON.stringify(wxShare) : "";
      wxx.wptCheckShare(wxShareJson);
}

function checkShareScreen(){
      var wxShareJson = window.wxShare ? JSON.stringify(wxShare) : "";
      wxx.wptCheckShareScreen(wxShareJson);
}

var respWptJumpNativeWithUrl = function () {};
function myRespWptJumpNativeWithUrl(o) {
    respWptJumpNativeWithUrl(o);
}

function wptJumpNativeWithUrl(url,cb,o) {
    respWptJumpNativeWithUrl = cb;
    var jumpNativeData = o ? JSON.stringify(o) : "";
    wxx.wptJumpNativeWithUrl(url,jumpNativeData);
}

var successChildLogin = function () {};
var failChildLogin = function () {};
function mySuccessChildLogin(){
   successChildLogin();
}

function myFailChildLogin(){
   failChildLogin();
}

function wptChildAccountLogin(o,callback,failCallBack) {
     successChildLogin = callback;
     failChildLogin = failCallBack;
     var loginData = o ? JSON.stringify(o) : "";
     wxx.wptLoginSon(loginData);
}

var successChildLogout = function () {};
function mySuccessChildLogout(){
   successChildLogout();
}
function wptChildAccountLogout(o,callback) {
     successChildLogout = callback;
     wxx.wptLogoutSon();
}

var successCheckBankInstall = function () {};
function mySuccessCheckBankInstall(o){
   successCheckBankInstall(o);
}

var app = {
   wptAppChooseWXPay : function(o){
        successPay = o.success;
        cancelPay = o.cancel;
        failPay = o.fail;
        wxx.wptChooseWXPay(o.partnerid,o.timestamp,o.noncestr,o.prepayid,o.paysign,o.package);
   },
   wptAppChangeUser : function(o){
        successAppChange = o.success;
        cancelAppChange = o.cancel;
        failAppChange = o.fail;
        wxx.wptAppChangeUser(o.userInfoId , o.openid , o.type);
   },
   wptAppChooseAliPay : function(o){
        successPay = o.success;
        cancelPay = o.cancel;
        failPay = o.fail;
        wxx.wptChooseAliPay(o.chooseAliPayParam);
   },
   wptAppChooseQPay : function(o){
        successPay = o.success;
        cancelPay = o.cancel;
        failPay = o.fail;
        wxx.wptChooseQQPay(o.payParam.tokenId,o.payParam.sig,o.payParam.nonce);
   }
};



var wx ={
   openAddress:function(o){
       successAddress=o.success;
       wxx.wptOpenAddress();
   },
   scanQRCode:function(o){
       successCode=o.success;
       wxx.wptScanQRCode();
   },
   previewImage:function(o){
       wxx.wptPreviewImage(o.current , o.urls.toString());
   },
   hideOptionMenu:function(){
   },
   showOptionMenu:function(){
   },
   onMenuShareAppMessage:function(o){
   },
   onMenuShareTimeline:function(o){
   },
   chooseWXPay:function(o){
       successPay = o.success;
       cancelPay = o.cancel;
       wxx.wptChooseWXPay(o.timestamp, o.nonceStr,o.package,o.paySign);
   },
   downloadImage:function(o){
      successDownloadImage=o.success;
      wxx.wptDownloadImage(o.serverId);
   },
   checkBankInstall:function(o){
      successCheckBankInstall=o.success;
      wxx.wptCheckBankInstall();
   }
};

WPT.Model.share = function() {
    var wxShareJson = window.wxShare ? JSON.stringify(wxShare) : "";
    wxx.wptShare(wxShareJson,"");
};

WPT.showLogin = function (successCallBack, failCallBack) {
    successLogin = successCallBack;
    failLogin = failCallBack;
    wxx.login();
};

var successLogout = function () {};
WPT.logout = function(o){
   successLogout = o.success;
   wxx.wptLogout();
};

WPT.playVideo = function(o,src){
    wxx.playVideo(src);
};

var successVideo = function () {};
var progressVideo = function () {};
var failVideo = function () {};
function mySuccessVideo(key){
   successVideo(key);
};
function myProgressVideo(progress){
   progressVideo(progress);
};
function myFailVideo(fail){
   failVideo(fail);
};
WPT.uploadVideo = function(o){
   successVideo = o.onSuccess;
   progressVideo = o.onProgress;
   failVideo = o.onFail; 
   wxx.wptUploadVideo(o.videoLimit,o.bType);
};
var successMultiImg = function () {};
WPT.uploadImg = function(o){
   successMultiImg = o.success;
   wxx.wptUploadImgMulti(o.count,o.isQr);
};
function mySuccessMultiImg(data){
   var objData = JSON.parse(data);
   successMultiImg(objData);
};
function triggerAppEvent(eventName, params){
    $(document).trigger('app-event-' + eventName, params);
};

function wptAppSetCookie(name, value, expiredays, domain) {
    var exdate = {};
    var domain = domain || '.weipaitang.com';
    if (typeof expiredays === 'number') {
        exdate = new Date();
        exdate.setDate(exdate.getDate() + expiredays);
    } else if (typeof expiredays === 'object') {
        exdate = expiredays;
    }
    var expireStr = expiredays == null ? ';domain=' + domain : ';domain=' + domain + ';expires=' + exdate.toGMTString();
    document.cookie = name + '=' + encodeURIComponent(value) + expireStr + ';path=/';
}



$(document).on('Page:change', function (e, url) {
    wxx && wxx.pageChange && wxx.pageChange();
});
$(document).trigger('wptLoadJs:success', {});
